package com.jingmi.jingmiliving.commodity.service.impl;

import com.jingmi.jingmiliving.commodity.vo.Catalog2Vo;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingmi.common.utils.PageUtils;
import com.jingmi.common.utils.Query;

import com.jingmi.jingmiliving.commodity.dao.CategoryDao;
import com.jingmi.jingmiliving.commodity.entity.CategoryEntity;
import com.jingmi.jingmiliving.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()

        );

        return new PageUtils(page);
    }



    @Override
    public Long[] getcascaderCategoryIds(Long categoryId) {
        ArrayList<Long> ids = new ArrayList<>();
        getcascaderCategoryParentsIds(categoryId,ids);
        Collections.reverse(ids);
         return ids.toArray(new Long [ids.size()] );

    }

    @Override
    public List<CategoryEntity> getLevel1Categorys() {
        List<CategoryEntity> categoryEntities = this.baseMapper.selectList(
                new QueryWrapper<CategoryEntity>().eq("parent_id", 0));
        return categoryEntities;
    }



    public List<Long> getcascaderCategoryParentsIds(Long categoryId,List<Long>ids){
        //先把自己添加进数组
        ids.add(categoryId);
        CategoryEntity childrenCategory = this.getById(categoryId);
        //获取父类id
        Long parentId = childrenCategory.getParentId();
        //判断是不是顶级菜单不是则递归调用添加进数组
        if (!parentId.equals(0L)){
            getcascaderCategoryParentsIds(parentId,ids);
        }
        return ids;

    }

    @Override
    public List<CategoryEntity> listTree() {
        //baseMapper 是从本类父类继承的 使用了泛型自动封装成 categoryEntity实体类
        //查询出所有数据
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //找出一级目录
        List<CategoryEntity> categoryTree = categoryEntities.stream().filter(/*对每个商品类排查是否为一级目录*/categoryEntity -> {
            //返回的是集合哦
            return categoryEntity.getParentId() ==0;
        }).map(/*对父集合设置子目录*/ categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategoryEntity(categoryEntity,categoryEntities));
            //设置好子目录返回父目录 这时候子目录已经挂载好了
            return categoryEntity;
        }).sorted(/*集合排序*/(categoryEntity1,categoryEntity2)->{
            return (categoryEntity1.getSort()==null?0:categoryEntity1.getSort())-
                    (categoryEntity2.getSort()==null?0:categoryEntity2.getSort());
        } ).collect(Collectors.toList());
        /*对流转换为集合*/
        return categoryTree;
    }



    /*一个是父级一个是全部实体递归找出
     * 并返回子目录集合*/
    public List<CategoryEntity> getChildrenCategoryEntity(CategoryEntity root,List<CategoryEntity> AllCategory){
        /*对全部过滤出目标的子目录*/
        List<CategoryEntity> collect = AllCategory.stream().filter(categoryEntity -> {
            /*找到了子目录返回*/
            return categoryEntity.getParentId().equals(root.getId());
        }).map(/*对子目录映射看看有没有子子目录*/ categoryEntity -> {
            categoryEntity.setChildrenCategories(/*递归调用*/getChildrenCategoryEntity(categoryEntity, AllCategory));
            return categoryEntity;
        }).sorted(/*集合排序*/(categoryEntity1, categoryEntity2) -> {
            return (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) -
                    (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort());
        }).collect(Collectors.toList());
        //返回子目录集合
        return collect;
    }

    @Override
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        System.out.println("进入方法===》");
        //这是全部的
        List<CategoryEntity> selectList = this.baseMapper.selectList(null);
        //这是顶级的categories
        List<CategoryEntity> level1Categorys = getParent_cid(selectList, 0L);

        Map<String, List<Catalog2Vo>> collect = level1Categorys.stream().collect(Collectors.toMap(
                k -> {
                    return k.getId().toString();
                }, v -> {
                    //这是map的value
                    List<Catalog2Vo> catalog2Vos = new ArrayList<>();
                    //取出实体类的值
                    List<CategoryEntity> level2Categories = getParent_cid(selectList, v.getId());
                    if (level2Categories != null && level2Categories.size() > 0){
                        //添加进去 value
                         catalog2Vos = level2Categories.stream().map(l2 -> {
                            //循环添加进去 l2的值
                            Catalog2Vo catalog2Vo =
                                    new Catalog2Vo(v.getId().toString(), null, l2.getId().toString(), l2.getName());
                             //把所有l3找出来
                            List<CategoryEntity> level3Categories = getParent_cid(selectList, l2.getId());
                            //找完了
                            List<Catalog2Vo.Category3Vo> category3Vos = level3Categories.stream().map(l3 -> {
                                //循环添加进去 l3的值
                                Catalog2Vo.Category3Vo category3Vo = new Catalog2Vo.Category3Vo(l2.getId().toString(), l3.getId().toString(), l3.getName());
                                return category3Vo;
                            }).collect(Collectors.toList());
                            catalog2Vo.setCatalog3List(category3Vos);
                            return catalog2Vo;
                        }).collect(Collectors.toList());
                    }


                    return catalog2Vos;
                }
        ));
                     return collect;

    }
    private List<CategoryEntity>
    getParent_cid(List<CategoryEntity> selectList,Long parentCid) {
        List<CategoryEntity> categoryEntities = selectList.stream().filter(item ->
                item.getParentId().equals(parentCid)).collect(Collectors.toList());
        return categoryEntities;
    }

    }